﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;

namespace StudentWeb.BLL
{
    public class XhReservationBO
    {
        #region 课程信息 分页列表
        /// <summary>
        /// 图书信息 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wc"></param>
        /// <returns></returns>
        public IResult GetXhReservationList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new XhReservationDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult<PagedList<XhReservationData>>(list);
        }
        #endregion
        #region 课程信息 单条记录
        /// <summary>
        /// 图书信息 单条记录
        /// </summary>
        /// <param name="XhReservationDataID"></param>
        /// <returns></returns>
        public IResult GetXhReservationInfo(int XhReservationDataID)
        {
            var res = new XhReservationDataDAO().GetInfo(XhReservationDataID);
            return new DataResult<XhReservationData>(res);
        }
        #endregion
        #region 课程信息 保存
        public bool SaveXhReservationInfo(dynamic data, int userid)
        {
            int xhreservationDataID = data.XhReservationDataID ?? 0;
            var result = 0;
            if (xhreservationDataID > 0)
            {
                var record = new XhReservationDataDAO().GetInfo(xhreservationDataID);
                //XhReservationID

                //CourseState课程状态
                record.XhVipName = data.XhVipName;//课程名称

                record.XhReservedDuration = data.XhReservedDuration;//开设学期

                record.XhReservedField = data.XhReservedField;//预约场地名称
                record.XhReservationState = data.XhReservationState;
                record.XhReservedTime = DateTime.Now;



                result = new XhReservationDataDAO().Update(record);
            }
            else if (xhreservationDataID == 0)
            {
                var xhreservation = new XhReservationData()
                {
                    //CourseDataID=自动
                    //CourseDataID=自动

                    XhReservationState = 1.ToString(),

                    XhVipName = data.XhVipName,


                    XhReservedDuration = data.XhReservedDuration,
                    XhReservedField = data.XhReservedField,


                    XhReservedTime = DateTime.Now,


                };
                result = new XhReservationDataDAO().Insert(xhreservation);
            }
            return result > 0;
        }
        #endregion
        #region 用户信息 删除
        public IResult DeleteVipInfo(int XhReservationDataID)
        {
            var res = new XhReservationDataDAO().Delete(XhReservationDataID);
            if (res > 0)
            {
                return new Result(ResultCode.Success, "删除成功");
            }
            else
            {
                return new Result(ResultCode.Fail, "删除失败");
            }
        }
        #endregion
        #region SetXhReservationState 设置图书状态
        public IResult SetXhReservationState(int xhreservationDataID, int xhreservationState)
        {
            var record = new XhReservationDataDAO().GetInfo(xhreservationDataID);
            if (record == null)
            {
                return new Result(ResultCode.Fail, "未找到此ID的预约");
            }
            int newState = 0;
            if (xhreservationState == 1)
            {
                newState = 2;
            }
            else if (xhreservationState == 2)
            {
                newState = 1;
            }
            record.XhReservationState = newState.ToString();

            var result = new XhReservationDataDAO().Update(record);
            return result >= 1 ? new Result() : new Result(ResultCode.Fail, "修改失败");
        }
        #endregion
    }
}
